package com.yifan.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.yifan.entity.Comment;
import com.yifan.service.CommentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/comment")
public class CommentServlet extends HttpServlet{
	
	CommentService commentService = new CommentService();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
		doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		//获取请求的方法
		String method = request.getParameter("method");
		//请求分发
		if("getComments".equals(method)){
			getComments(request, response);
		}else if("getComments2".equals(method)){
			getComments2(request, response);
		}else if("getComment".equals(method)) {
			getComment(request, response);
		}else if("updateComment".equals(method)) {
			updateComment(request, response);
		}else if("addComment".equals(method)) {
			addComment(request, response);
		}else if("deleteComment".equals(method)) {
			deleteComment(request, response);
		}else if("deleteComments".equals(method)) {
			deleteComments(request, response);
		}

	}

	private void getComments(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		response.setCharacterEncoding("utf-8");
		String username = request.getParameter("realname");
		String softwareId = request.getParameter("softwareId");
		//获取分页参数
		int page = 1;//Integer.parseInt(request.getParameter("pageIndex"));
		int pageSize = 76;//Integer.parseInt(request.getParameter("pageSize"));
		
		Map<String, Object> comments = commentService.getComments(username, softwareId, page, pageSize);
		// response.getWriter().write(JSON.toJSONStringWithDateFormat(comments,"yyyy-MM-dd HH:mm:ss",SerializerFeature.WriteDateUseDateFormat));
		request.setAttribute("comments", comments.get("data"));
		request.getRequestDispatcher("/comment.jsp").forward(request,response);
	}

	private void getComments2(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		response.setCharacterEncoding("utf-8");
		String userid = request.getSession().getAttribute("userid").toString();
		String softwareId = request.getParameter("softwareId");
		//获取分页参数
		int page = 1;//Integer.parseInt(request.getParameter("pageIndex"));
		int pageSize = 76;//Integer.parseInt(request.getParameter("pageSize"));

		Map<String, Object> comments = commentService.getComments(userid, softwareId, page, pageSize);
		// response.getWriter().write(JSON.toJSONStringWithDateFormat(comments,"yyyy-MM-dd HH:mm:ss",SerializerFeature.WriteDateUseDateFormat));
		request.setAttribute("comments", comments.get("data"));
		request.getRequestDispatcher("/comment2.jsp").forward(request,response);
	}

	private void getComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		Map<String, Object> comment = commentService.getComment(id);
		System.out.println(JSON.toJSON(comment));
		response.getWriter().write(JSON.toJSONStringWithDateFormat(comment,"yyyy-MM-dd HH:mm:ss",SerializerFeature.WriteDateUseDateFormat));
	}
	
	private void updateComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		String userId = request.getParameter("userId");
		String softwareId = request.getParameter("softwareId");
		String content = request.getParameter("content");
		String createDate = request.getParameter("createDate");
		Comment comment = new Comment(id, Integer.parseInt(userId), Integer.parseInt(softwareId), content, createDate);
		Map<String, String> map = commentService.updateComment(comment);
		response.getWriter().write(JSON.toJSON(map).toString());
	}
	
	private void deleteComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		
		Map<String, String> map = commentService.deleteComment(id);
		response.getWriter().write(JSON.toJSON(map).toString());
	}
	
	private void deleteComments(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String ids = request.getParameter("ids");
		String[] idarr = ids.substring(0, ids.length()-1).split(",");
		System.out.println(idarr.toString());
		Map<String, String> map = commentService.deleteComments(idarr);
		response.getWriter().write(JSON.toJSON(map).toString());
	}
	
	private void addComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String userId = request.getParameter("userId");
		String softwareId = request.getParameter("softwareId");
		String content = request.getParameter("content");
		String createDate = request.getParameter("createDate");
		Comment comment = new Comment(0, Integer.parseInt(userId), Integer.parseInt(softwareId), content, createDate);
		Map<String, String> map = commentService.addComment(comment);
		response.getWriter().write(JSON.toJSON(map).toString());
	}
}
